import mysocks
import string
import re

class irccmds:
  def __init__(self, socks=None, socklen=int(8)):
    if socks is None:
      self.socks = mysocks.socks()
    else:
      self.socks = socks
    if socklen == 8:
      self.socks.socklen = 8
    else:
      self.socks.socklen = socklen

  def connect(self, server, port):
    self.socks.myconnect(server, port)

  def nick(self, nick):
    self.socks.send("NICK %s\r\n" % nick)

  def user(self, ident, server, realname):
    self.socks.send("USER %s %s bla :%s\r\n" % (ident, server, realname))

  def join(self, channel):
    self.socks.send("JOIN %s\r\n" % channel)

  def privmsg(self, channel, msg):
    self.socks.send("PRIVMSG %s : %s\r\n" % (channel, msg))

  def quit(self, reason):
    self.socks.send("QUIT : %s\r\n" % reason)

  def pong(self, pong):
    self.socks.send("PONG %s\r\n" % pong)

  def op(self, channel, usr):
    self.socks.send("MODE %s +o %s\r\n" % (channel, usr))

class ircparse:

  def __init__(self, cmds=None):
    if cmds is None:
      self.irccmds = irccmds()
    else:
      self.irccmds = cmds

  def findident(self, a):
    search = re.search('(!)(~)?(\S+@\S+(\S))', a)
    if search is None:
      return None
    else:
      return search.group(0)

  def getirccmd(self, a):
    ident = self.findident(a)
    if ident is None:
      return None
    else:
      line = a.replace(ident, ":", 1)
      line = line.split(":", 3)
      line = line[2]
      line = line.split()
      cmd = line[0]
      return cmd

  def getchan(self, a):
    ident = self.findident(a)
    if ident is None:
      return None
    else:
      line = a.replace(ident, ":", 1)
      line = line.split(":", 3)
      line = line[2]
      line = line.split()
      chan = line[1]
      return chan

  def getcmd(self, a):
    ident = self.findident(a)
    print ident
    if ident is None:
      return None
    else:
      line = a.replace(ident, ":", 1)
      line = line.split(":", 3)
      line = line[-1]
      line = line.split()
      cmd = line[1]
      print cmd
      return cmd

  def getbotcall(self, a):
    ident = self.findident(a)
    if ident is None:
      return None
    else:
      line = a.replace(ident, ":", 1)
      line = line.split(":", 3)
      line = line[-1]
      line = line.split()
      botcall = line[0]
      return botcall

  def getusr(self, a):
    ident = self.findident(a)
    if ident is None:
      return None
    else:
      line = a.replace(ident, ":", 1)
      line = line.split(":", 3)
      usr = line[1]
      return usr
